import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:vape_xe/routes/app_pages.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/utils.dart';
import 'package:vape_xe/widgets/widgets.dart';

import 'logic.dart';

class TeamView extends GetView<TeamController> {
  const TeamView({super.key});

  Widget _inviteItem(int index) {
    return Container(
      width: 300.w,
      height: 64.h,
      decoration: BoxDecoration(
        borderRadius: 12.radius,
        color: Colors.white10,
      ),
      margin: EdgeInsets.only(left: 16.w, bottom: 12.h, right: 16.w),
      child: Row(
        children: [
          16.vGap,
          netImageCached(
            controller.teamList[index]['avatar'],
            width: 42.w,
            height: 42.w,
            radius: 100.w,
            fit: BoxFit.cover,
          ),
          12.vGap,
          Text(
            controller.teamList[index]['nickName'],
            style: TextStyle(
              fontSize: 12.sp,
              fontFamily: 'Montserrat',
              fontWeight: FontWeight.bold,
            ),
          ),
          16.vGap
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new_rounded),
          onPressed: () => {Navigator.of(context).pop('刷新')},
        ),
        title: Text("My team members".tr),
      ),
      body: SmartRefresher(
        enablePullUp: true,
        physics: const BouncingScrollPhysics(),
        controller: controller.refreshController,
        onRefresh: controller.onRefresh,
        onLoading: controller.onLoading,
        child: CustomScrollView(slivers: [
          SliverToBoxAdapter(
            child: Column(
              children: [
                50.hGap,
                Hero(
                  tag: 'hero_avatar',
                  child: netImageCached(
                    UserStore.to.profile.avatar ?? '',
                    width: 72.w,
                    height: 72.w,
                    radius: 100.w,
                    fit: BoxFit.cover,
                    showProgress: false,
                  ),
                ),
                18.hGap,
                Hero(
                  tag: 'hero_nickname',
                  child: Text(UserStore.to.profile.nickName ?? '', style: TextStyle(fontSize: 16.sp)),
                ),
                20.hGap,
                Container(
                  height: 48.h,
                  margin: EdgeInsets.symmetric(horizontal: 16.w),
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  decoration: BoxDecoration(
                    borderRadius: 12.radius,
                    color: Colors.white10,
                  ),
                  child: Row(
                    children: [
                      Text(
                        '${"Invitation code".tr}：',
                        style: const TextStyle(fontWeight: FontWeight.bold),
                      ),
                      GestureDetector(
                        onTap: () => UserStore.to.profile.inviteCode?.copy(),
                        child: Row(children: [
                          Text(
                            UserStore.to.profile.inviteCode ?? '',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              foreground: Paint()
                                ..shader = const LinearGradient(
                                  colors: <Color>[Color(0xffF5B946), Color(0xffF58546)],
                                ).createShader(
                                  const Rect.fromLTWH(0.0, 0.0, 150.0, 0.0),
                                ),
                            ),
                          ),
                          8.vGap,
                          Image.asset(
                            'assets/images/ic_wenhao.webp',
                            height: 14.w,
                            width: 14.w,
                          ),
                        ]),
                      ),
                      const Spacer(),
                      InkWell(
                        onTap: () => Get.bottomSheet(const ShareDialogView()),
                        child: Image.asset(
                          'assets/images/ic_share.webp',
                          height: 28.w,
                          width: 28.w,
                        ),
                      )
                    ],
                  ),
                ),
                40.hGap,
                Row(
                  children: [
                    16.vGap,
                    Text('My team'.tr,
                        style: TextStyle(fontSize: 20.sp, fontFamily: 'Montserrat', fontWeight: FontWeight.bold)),
                    const Spacer(),
                    GestureDetector(
                      onTap: () => Get.toNamed(Routes.PROCEEDS),
                      child: Text('Details of proceeds'.tr),
                    ),
                    16.vGap,
                  ],
                ),
                20.hGap
              ],
            ),
          ),
          Obx(
            () => controller.teamList.isEmpty
                ? SliverToBoxAdapter(
                    child: SizedBox(
                      height: 200.h,
                      child: Center(
                        child: Text('Empty list'.tr),
                      ),
                    ),
                  )
                : SliverList(
                    delegate: SliverChildBuilderDelegate(
                      childCount: controller.teamList.length,
                      (context, index) => _inviteItem(index),
                    ),
                  ),
          )
        ]),
      ),
    );
  }
}
